<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>跨域 &amp; CORS &amp; JSONP | Guru Note</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="stylesheet" href="./font/index.css">
    <script src="/docs/icon/svg.js"></script>
    <meta name="description" content="Sea of dawn">
    
    <link rel="preload" href="/docs/assets/css/0.styles.efa081cd.css" as="style"><link rel="preload" href="/docs/assets/js/app.cdec4db9.js" as="script"><link rel="preload" href="/docs/assets/js/2.993bd611.js" as="script"><link rel="preload" href="/docs/assets/js/85.e9d7c0d1.js" as="script"><link rel="preload" href="/docs/assets/js/7.5a12abef.js" as="script"><link rel="prefetch" href="/docs/assets/js/10.c3177bdb.js"><link rel="prefetch" href="/docs/assets/js/100.60c23478.js"><link rel="prefetch" href="/docs/assets/js/101.7ea29e1b.js"><link rel="prefetch" href="/docs/assets/js/102.7f47e485.js"><link rel="prefetch" href="/docs/assets/js/103.ea9ce400.js"><link rel="prefetch" href="/docs/assets/js/104.4ec6a518.js"><link rel="prefetch" href="/docs/assets/js/105.75526347.js"><link rel="prefetch" href="/docs/assets/js/106.01f6b03b.js"><link rel="prefetch" href="/docs/assets/js/107.69fe0811.js"><link rel="prefetch" href="/docs/assets/js/108.fa23768b.js"><link rel="prefetch" href="/docs/assets/js/109.90df1698.js"><link rel="prefetch" href="/docs/assets/js/11.54347528.js"><link rel="prefetch" href="/docs/assets/js/110.dd8d7227.js"><link rel="prefetch" href="/docs/assets/js/111.ccf25ceb.js"><link rel="prefetch" href="/docs/assets/js/112.6963298f.js"><link rel="prefetch" href="/docs/assets/js/113.30ceb3d8.js"><link rel="prefetch" href="/docs/assets/js/114.42ef6603.js"><link rel="prefetch" href="/docs/assets/js/115.f1db7817.js"><link rel="prefetch" href="/docs/assets/js/116.92971223.js"><link rel="prefetch" href="/docs/assets/js/117.c5a65e7e.js"><link rel="prefetch" href="/docs/assets/js/118.e329035d.js"><link rel="prefetch" href="/docs/assets/js/119.cbb17c5e.js"><link rel="prefetch" href="/docs/assets/js/12.c8144ee8.js"><link rel="prefetch" href="/docs/assets/js/120.8537f6a6.js"><link rel="prefetch" href="/docs/assets/js/121.257d3851.js"><link rel="prefetch" href="/docs/assets/js/122.96a5f921.js"><link rel="prefetch" href="/docs/assets/js/123.2220fd12.js"><link rel="prefetch" href="/docs/assets/js/124.552b1a29.js"><link rel="prefetch" href="/docs/assets/js/125.4e36fe37.js"><link rel="prefetch" href="/docs/assets/js/126.3939cdcc.js"><link rel="prefetch" href="/docs/assets/js/127.014df434.js"><link rel="prefetch" href="/docs/assets/js/13.edc237a8.js"><link rel="prefetch" href="/docs/assets/js/14.66270d4f.js"><link rel="prefetch" href="/docs/assets/js/15.0804164b.js"><link rel="prefetch" href="/docs/assets/js/16.dec928ab.js"><link rel="prefetch" href="/docs/assets/js/17.bbfc90a8.js"><link rel="prefetch" href="/docs/assets/js/18.76904860.js"><link rel="prefetch" href="/docs/assets/js/19.80a98011.js"><link rel="prefetch" href="/docs/assets/js/20.c39e42bd.js"><link rel="prefetch" href="/docs/assets/js/21.287b744a.js"><link rel="prefetch" href="/docs/assets/js/22.c9506be7.js"><link rel="prefetch" href="/docs/assets/js/23.9732a229.js"><link rel="prefetch" href="/docs/assets/js/24.21ed1f05.js"><link rel="prefetch" href="/docs/assets/js/25.8f1926f6.js"><link rel="prefetch" href="/docs/assets/js/26.61b6eb9f.js"><link rel="prefetch" href="/docs/assets/js/27.3706753c.js"><link rel="prefetch" href="/docs/assets/js/28.b6b182cf.js"><link rel="prefetch" href="/docs/assets/js/29.ae979ad9.js"><link rel="prefetch" href="/docs/assets/js/3.a7f8dd77.js"><link rel="prefetch" href="/docs/assets/js/30.f2233269.js"><link rel="prefetch" href="/docs/assets/js/31.06780314.js"><link rel="prefetch" href="/docs/assets/js/32.2dad91d1.js"><link rel="prefetch" href="/docs/assets/js/33.87ec6e22.js"><link rel="prefetch" href="/docs/assets/js/34.7b75f220.js"><link rel="prefetch" href="/docs/assets/js/35.3184991f.js"><link rel="prefetch" href="/docs/assets/js/36.28248fbe.js"><link rel="prefetch" href="/docs/assets/js/37.5824a979.js"><link rel="prefetch" href="/docs/assets/js/38.99a364fe.js"><link rel="prefetch" href="/docs/assets/js/39.a5f43732.js"><link rel="prefetch" href="/docs/assets/js/4.a55d89ed.js"><link rel="prefetch" href="/docs/assets/js/40.629f78f7.js"><link rel="prefetch" href="/docs/assets/js/41.e4f58d1c.js"><link rel="prefetch" href="/docs/assets/js/42.9f41aa47.js"><link rel="prefetch" href="/docs/assets/js/43.636412b6.js"><link rel="prefetch" href="/docs/assets/js/44.3b491aef.js"><link rel="prefetch" href="/docs/assets/js/45.77df19bc.js"><link rel="prefetch" href="/docs/assets/js/46.01f53ddd.js"><link rel="prefetch" href="/docs/assets/js/47.476e85c9.js"><link rel="prefetch" href="/docs/assets/js/48.198502dc.js"><link rel="prefetch" href="/docs/assets/js/49.0d59d332.js"><link rel="prefetch" href="/docs/assets/js/5.1597c0f8.js"><link rel="prefetch" href="/docs/assets/js/50.02baf101.js"><link rel="prefetch" href="/docs/assets/js/51.c9fbd54d.js"><link rel="prefetch" href="/docs/assets/js/52.4cb4459b.js"><link rel="prefetch" href="/docs/assets/js/53.248450d7.js"><link rel="prefetch" href="/docs/assets/js/54.83f12d1e.js"><link rel="prefetch" href="/docs/assets/js/55.b18accba.js"><link rel="prefetch" href="/docs/assets/js/56.8a160b09.js"><link rel="prefetch" href="/docs/assets/js/57.b854a940.js"><link rel="prefetch" href="/docs/assets/js/58.4500f315.js"><link rel="prefetch" href="/docs/assets/js/59.59400e36.js"><link rel="prefetch" href="/docs/assets/js/6.887ba020.js"><link rel="prefetch" href="/docs/assets/js/60.4dd5b5bb.js"><link rel="prefetch" href="/docs/assets/js/61.50d8c8f6.js"><link rel="prefetch" href="/docs/assets/js/62.17d10daa.js"><link rel="prefetch" href="/docs/assets/js/63.d5f821cc.js"><link rel="prefetch" href="/docs/assets/js/64.7bf2519f.js"><link rel="prefetch" href="/docs/assets/js/65.0a1c9bc8.js"><link rel="prefetch" href="/docs/assets/js/66.1bcaa81c.js"><link rel="prefetch" href="/docs/assets/js/67.326bdf9b.js"><link rel="prefetch" href="/docs/assets/js/68.2b3b63b2.js"><link rel="prefetch" href="/docs/assets/js/69.c9b1a1a9.js"><link rel="prefetch" href="/docs/assets/js/70.a5fede78.js"><link rel="prefetch" href="/docs/assets/js/71.288d9643.js"><link rel="prefetch" href="/docs/assets/js/72.8665d6b8.js"><link rel="prefetch" href="/docs/assets/js/73.47155429.js"><link rel="prefetch" href="/docs/assets/js/74.4d4738f8.js"><link rel="prefetch" href="/docs/assets/js/75.659a325f.js"><link rel="prefetch" href="/docs/assets/js/76.a263f692.js"><link rel="prefetch" href="/docs/assets/js/77.9d86d59e.js"><link rel="prefetch" href="/docs/assets/js/78.a41b5d88.js"><link rel="prefetch" href="/docs/assets/js/79.5d0fc4cc.js"><link rel="prefetch" href="/docs/assets/js/8.80d0c81d.js"><link rel="prefetch" href="/docs/assets/js/80.01a11f6a.js"><link rel="prefetch" href="/docs/assets/js/81.9c0207d9.js"><link rel="prefetch" href="/docs/assets/js/82.12be8e04.js"><link rel="prefetch" href="/docs/assets/js/83.00fa5f46.js"><link rel="prefetch" href="/docs/assets/js/84.f14cd53c.js"><link rel="prefetch" href="/docs/assets/js/86.eb78fbbf.js"><link rel="prefetch" href="/docs/assets/js/87.8a31f5ae.js"><link rel="prefetch" href="/docs/assets/js/88.0f10e6dc.js"><link rel="prefetch" href="/docs/assets/js/89.a17253c9.js"><link rel="prefetch" href="/docs/assets/js/9.2fa568cb.js"><link rel="prefetch" href="/docs/assets/js/90.e56bc609.js"><link rel="prefetch" href="/docs/assets/js/91.5277db18.js"><link rel="prefetch" href="/docs/assets/js/92.b0bb8aa7.js"><link rel="prefetch" href="/docs/assets/js/93.cd790b77.js"><link rel="prefetch" href="/docs/assets/js/94.76bbe227.js"><link rel="prefetch" href="/docs/assets/js/95.4f07ca32.js"><link rel="prefetch" href="/docs/assets/js/96.2b1e8485.js"><link rel="prefetch" href="/docs/assets/js/97.e93b3461.js"><link rel="prefetch" href="/docs/assets/js/98.658063a1.js"><link rel="prefetch" href="/docs/assets/js/99.a3642e6d.js">
    <link rel="stylesheet" href="/docs/assets/css/0.styles.efa081cd.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/docs/" class="home-link router-link-active"><!----> <span class="site-name">Guru Note</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----> <svg aria-hidden="true" class="icon search-icon"><use xlink:href="#icon-search"></use></svg></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/docs/layout/" class="nav-link">
  CSS 手册
</a></div><div class="nav-item"><a href="/docs/js/" class="nav-link router-link-active">
  JavaScript
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端进阶" class="dropdown-title"><span class="title">前端进阶</span> <span class="arrow down"></span></button> <button type="button" aria-label="前端进阶" class="mobile-dropdown-title"><span class="title">前端进阶</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          前端框架
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/vue/" class="nav-link">
  Vue.js
</a></li><li class="dropdown-subitem"><a href="/docs/react/" class="nav-link">
  React.js
</a></li></ul></li><li class="dropdown-item"><h4>
          拓展语言
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/node/" class="nav-link">
  Node.js
</a></li><li class="dropdown-subitem"><a href="/docs/ts/" class="nav-link">
  TypeScript
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/docs/devbook/" class="nav-link">
  开发手册
</a></div><div class="nav-item"><a href="/docs/exam/" class="nav-link">
  金九银十
</a></div><div class="nav-item"><a href="/docs/message/" class="nav-link">
  留言板
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><div class="el-scrollbar"><div class="scrollbar-wrapper el-scrollbar__wrap el-scrollbar__wrap--hidden-default"><div class="el-scrollbar__view"><nav class="nav-links"><div class="nav-item"><a href="/docs/layout/" class="nav-link">
  CSS 手册
</a></div><div class="nav-item"><a href="/docs/js/" class="nav-link router-link-active">
  JavaScript
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端进阶" class="dropdown-title"><span class="title">前端进阶</span> <span class="arrow down"></span></button> <button type="button" aria-label="前端进阶" class="mobile-dropdown-title"><span class="title">前端进阶</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          前端框架
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/vue/" class="nav-link">
  Vue.js
</a></li><li class="dropdown-subitem"><a href="/docs/react/" class="nav-link">
  React.js
</a></li></ul></li><li class="dropdown-item"><h4>
          拓展语言
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/node/" class="nav-link">
  Node.js
</a></li><li class="dropdown-subitem"><a href="/docs/ts/" class="nav-link">
  TypeScript
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/docs/devbook/" class="nav-link">
  开发手册
</a></div><div class="nav-item"><a href="/docs/exam/" class="nav-link">
  金九银十
</a></div><div class="nav-item"><a href="/docs/message/" class="nav-link">
  留言板
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/docs/js/" aria-current="page" class="sidebar-link">介绍</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>JavaScript</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/js/base/type-of-data.html" class="sidebar-link">JS 的数据类型</a></li><li><a href="/docs/js/base/object.html" class="sidebar-link">JS 对象的常用属性</a></li><li><a href="/docs/js/base/function.html" class="sidebar-link">函数 &amp; 原型链</a></li><li><a href="/docs/js/base/dom-api.html" class="sidebar-link">DOM API 总结</a></li><li><a href="/docs/js/base/node-api.html" class="sidebar-link">节点 API 总结</a></li><li><a href="/docs/js/base/node-list.html" class="sidebar-link">节点中的伪数组</a></li><li><a href="/docs/js/base/dom-operate-across-threads.html" class="sidebar-link">DOM 操作跨线程</a></li><li><a href="/docs/js/base/create-jquery.html" class="sidebar-link">jQuery 中的链式操作</a></li><li><a href="/docs/js/base/plugin.html" class="sidebar-link">JavaScript 插件 / 库</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>ES6</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/js/es6/scope.html" class="sidebar-link">作用域</a></li><li><a href="/docs/js/es6/new-object.html" class="sidebar-link">新版对象</a></li><li><a href="/docs/js/es6/anatomy-assignment.html" class="sidebar-link">参数处理 &amp; 解构赋值</a></li><li><a href="/docs/js/es6/new-string.html" class="sidebar-link">新版字符串</a></li><li><a href="/docs/js/es6/module-use.html" class="sidebar-link">ES6 模块化</a></li><li><a href="/docs/js/es6/promise-usage.html" class="sidebar-link">Promise</a></li><li><a href="/docs/js/es6/await-and-async.html" class="sidebar-link">async 和 await 的用法</a></li><li><a href="/docs/js/es6/symbol-iterate-generate.html" class="sidebar-link">Symbol &amp; 迭代器 &amp; 生成器</a></li><li><a href="/docs/js/es6/arrow-function.html" class="sidebar-link">箭头函数</a></li><li><a href="/docs/js/es6/new-api.html" class="sidebar-link">ES6 新增 API</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>JavaScript 进阶</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/js/part/ajax-use.html" class="sidebar-link">AJAX</a></li><li><a href="/docs/js/part/cross-domain.html" aria-current="page" class="active sidebar-link">跨域 &amp; CORS &amp; JSONP</a></li><li><a href="/docs/js/part/browser-storage.html" class="sidebar-link">浏览器模型</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>JS 小记</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/js/note/project.html" class="sidebar-link">JavaScript 搭建项目</a></li><li><a href="/docs/js/note/js-garbage.html" class="sidebar-link">JS 的缺陷</a></li><li><a href="/docs/js/note/setInterval-a-setTimeout.html" class="sidebar-link">使用 setTimeout 实现 setInterval</a></li><li><a href="/docs/js/note/array-deduplica.html" class="sidebar-link">数组去重</a></li><li><a href="/docs/js/note/download-file.html" class="sidebar-link">使用 JS 下载文件</a></li><li><a href="/docs/js/note/top-nav.html" class="sidebar-link">设置导航动态显示 / 隐藏</a></li><li><a href="/docs/js/note/anchor-transition.html" class="sidebar-link">JS 实现点击锚点过渡效果</a></li><li><a href="/docs/js/note/data-list.html" class="sidebar-link">数据处理</a></li></ul></section></li></ul> </div></div><div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="width:0;transform:translateX(0%);ms-transform:translateX(0%);webkit-transform:translateX(0%);"></div></div><div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="height:0;transform:translateY(0%);ms-transform:translateY(0%);webkit-transform:translateY(0%);"></div></div></div></aside> <main class="page"> <div class="re-page-top"><h1 class="title"><span>跨域 &amp; CORS &amp; JSONP</span></h1> <div class="re-page-index"><div class="page-index-title"><span class="open-catalog"><svg aria-hidden="true" class="icon arrow-right"><use xlink:href="#icon-bold-right"></use></svg> <span class="text">目录</span></span></div> <div class="page-index-content"><div class="page-catalog" style="display:none;"><div class="synopsis-wrap"><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#关键词" class="synopsis-link">关键词</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#同源策略" class="synopsis-link">同源策略</a><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#什么是源" class="synopsis-link">什么是源</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#什么是同源" class="synopsis-link">什么是同源</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#什么是同源策略" class="synopsis-link">什么是同源策略</a></li></ul></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#什么是跨域" class="synopsis-link">什么是跨域</a><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#为什么a-qq-com访问qq-com也算跨域" class="synopsis-link">为什么a.qq.com访问qq.com也算跨域？</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#为什么不同端口也算跨域" class="synopsis-link">为什么不同端口也算跨域？</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#为什么两个网站的ip是一样的-也算跨域" class="synopsis-link">为什么两个网站的IP是一样的，也算跨域？</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#为什么可以跨域使用css、js和图片等" class="synopsis-link">为什么可以跨域使用CSS、JS和图片等</a></li></ul></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#使用cors解决跨域问题" class="synopsis-link">使用CORS解决跨域问题</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#使用jsonp兼容ie跨域" class="synopsis-link">使用JSONP兼容IE跨域</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#总结" class="synopsis-link">总结</a><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#什么是jsonp" class="synopsis-link">什么是JSONP？</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#什么是跨域-2" class="synopsis-link">什么是跨域？</a></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#什么是cors" class="synopsis-link">什么是CORS？</a></li></ul></li><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#拓展" class="synopsis-link">拓展</a><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/js/part/cross-domain.html#json数据可视化插件" class="synopsis-link">JSON数据可视化插件</a></li></ul></li></ul></div></div></div></div></div> <div class="theme-default-content content__default"><h2 id="关键词"><a href="#关键词" class="header-anchor">#</a> 关键词</h2> <ul><li><p>同源策略</p> <p>浏览器故意设计的一个功能限制</p></li> <li><p>CORS</p> <p>突破浏览器限制的一个方法</p></li> <li><p>JSONP</p> <p>IE时代妥协的产物</p></li></ul> <h2 id="同源策略"><a href="#同源策略" class="header-anchor">#</a> 同源策略</h2> <h3 id="什么是源"><a href="#什么是源" class="header-anchor">#</a> 什么是源</h3> <ol><li><p>使用 window.origin 或 location.origin 可以得到当前页面的源</p></li> <li><p>源 = 协议 + 域名 + 端口号</p></li></ol> <h3 id="什么是同源"><a href="#什么是同源" class="header-anchor">#</a> 什么是同源</h3> <p>如果两个 url 的<strong>协议</strong>、<strong>域名</strong>、<strong>端口号</strong>完全一致，那么这两个 url 就是同源的</p> <p>举例</p> <div class="language-http extra-class"><pre class="language-http"><code><span class="token header-name keyword">https:</span>//www.qq.com/ 和 https://www.baidu.com/ 不同源

<span class="token header-name keyword">https:</span>//baidu.com/ 和 https://www.baidu.com/ 不同源
</code></pre></div><h3 id="什么是同源策略"><a href="#什么是同源策略" class="header-anchor">#</a> 什么是同源策略</h3> <p>如果JS运行在源A里，那么就只能获取源A的数据，不能获取源B的数据，即不允许跨域。</p> <p>就是不同源的页面之间的JS，不准互相请求数据。</p> <p>除了同源策略，其实根据请求头(Request Headers)的<code>referer</code>，也可以判断请求是否为当前域名。</p> <p>https://uphg.github.io/images/blog-referer.jpg</p> <h2 id="什么是跨域"><a href="#什么是跨域" class="header-anchor">#</a> 什么是跨域</h2> <p>当前页面下的JS发起的 AJAX 请求，一般情况下该请求只能是当前页面下同域名的请求，如果请求其他域名下内容就叫做跨域。</p> <h3 id="为什么a-qq-com访问qq-com也算跨域"><a href="#为什么a-qq-com访问qq-com也算跨域" class="header-anchor">#</a> 为什么a.qq.com访问qq.com也算跨域？</h3> <p>因为历史上出现过不同公司共用域名。a.qq.com 和 qq.com 不一定是同一个网站，浏览器为了谨慎起见，认为这是不同源。</p> <h3 id="为什么不同端口也算跨域"><a href="#为什么不同端口也算跨域" class="header-anchor">#</a> 为什么不同端口也算跨域？</h3> <p>一个端口一个公司。记住安全链条的强度取决于最弱的一环，任何安全相关的问题都要谨慎对待（就是浏览器限制比较严格）</p> <h3 id="为什么两个网站的ip是一样的-也算跨域"><a href="#为什么两个网站的ip是一样的-也算跨域" class="header-anchor">#</a> 为什么两个网站的IP是一样的，也算跨域？</h3> <p>原因同上，IP可以公用（还是浏览器限制比较严格）</p> <h3 id="为什么可以跨域使用css、js和图片等"><a href="#为什么可以跨域使用css、js和图片等" class="header-anchor">#</a> 为什么可以跨域使用CSS、JS和图片等</h3> <p>同源策略限制的是数据访问，我们引用CSS、JS和图片的时候，其实并不知道里面的内容是什么，我们只是在引用。并不能通过AJAX请求拿到里面的数据。</p> <p>可以看作是我们只是获取到它呈现的样子，并不是它本身的数据。</p> <h2 id="使用cors解决跨域问题"><a href="#使用cors解决跨域问题" class="header-anchor">#</a> 使用CORS解决跨域问题</h2> <p>响应首部中可以携带一个 Access-Control-Allow-Origin 字段，其语法如下:</p> <div class="language- extra-class"><pre class="language-text"><code>Access-Control-Allow-Origin: http://mozilla.com
</code></pre></div><blockquote><p>表示允许来自 http://mozilla.com 的请求</p></blockquote> <p>MDN文档: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS#简单请求</p> <h2 id="使用jsonp兼容ie跨域"><a href="#使用jsonp兼容ie跨域" class="header-anchor">#</a> 使用JSONP兼容IE跨域</h2> <p><strong>JSONP 和 JSON 并没有任何关系</strong></p> <p>我的理解：JSONP 就是利用后台在当前域名下的JS文件中动态写入一个JS数据，将这个数据传入 window.xxx 函数中(<code>window.xxx(数据)</code>)，其他域名需要跨域访问时就动态引入一个script标签将当前域名下的JS文件引入，引入后调用 window.xxx 函数，即可访问当前域名下的数据</p> <p><strong>封装一个JSONP函数</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">jsonp</span><span class="token punctuation">(</span><span class="token parameter">url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
        <span class="token keyword">const</span> random <span class="token operator">=</span> <span class="token string">&quot;chenJSONPCallbackName&quot;</span> <span class="token operator">+</span> Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        window<span class="token punctuation">[</span>random<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token parameter">data</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token function">resolve</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">const</span> script <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">'script'</span><span class="token punctuation">)</span>
        <span class="token comment">// 此处的 callback 是前后端默认约定俗成的名称</span>
        script<span class="token punctuation">.</span>src <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>url<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">?callback=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>random<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span>
        script<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
            <span class="token comment">// 如果请求成功,就删除 script 标签</span>
            script<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        script<span class="token punctuation">.</span><span class="token function-variable function">onerror</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
            <span class="token comment">// 如果请求失败就返回 reject() 函数</span>
            <span class="token function">reject</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        document<span class="token punctuation">.</span>body<span class="token punctuation">.</span><span class="token function">appendChild</span><span class="token punctuation">(</span>script<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// 使用</span>
<span class="token function">jsonp</span><span class="token punctuation">(</span><span class="token string">'http://qq.com/main.js'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h2> <h3 id="什么是jsonp"><a href="#什么是jsonp" class="header-anchor">#</a> 什么是JSONP？</h3> <p>JSONP 就是由于我们需要跨域请求时，当前浏览器不支持 CORS 或不兼容 CORS 等其他原因，我们必须使用另外一种方式来跨域。</p> <p>于是我们就跨域请求一个 JS 文件，这个 JS 文件会执行一个回调，回调里面就有我们需要的数据，回调的名称是可以随机生成的，可以给一个随机数，然后把这个名字用一个名叫 callback 参数传给后台，后台会把这个函数再次返回给我们并执行。</p> <p>优点: 兼容IE，它可以跨域。</p> <p>缺点: 由于它是利用 script 标签获取，所以它只能发送GET请求，它不支持POST请求。</p> <h3 id="什么是跨域-2"><a href="#什么是跨域-2" class="header-anchor">#</a> 什么是跨域？</h3> <p>域名A下的JS想通过AJAX请求域名B的数据，就叫做跨域。</p> <h3 id="什么是cors"><a href="#什么是cors" class="header-anchor">#</a> 什么是CORS？</h3> <p>CORS就是用来解决跨域请求的一种方式，在响应头携带一个 Access-Control-Allow-Origin 字段，表明指定的域名可以访问该网站。</p> <h2 id="拓展"><a href="#拓展" class="header-anchor">#</a> 拓展</h2> <h3 id="json数据可视化插件"><a href="#json数据可视化插件" class="header-anchor">#</a> JSON数据可视化插件</h3> <p>去 Chrome 网上应用店下载 <code>JSON Viewer</code> 插件</p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新：</span><span class="time">2021-02-10 12:44</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><svg aria-hidden="true" class="icon"><use xlink:href="#icon-left"></use></svg> <a href="/docs/js/part/ajax-use.html" class="prev">AJAX</a></span> <span class="next"><a href="/docs/js/part/browser-storage.html">浏览器模型</a> <svg aria-hidden="true" class="icon"><use xlink:href="#icon-right"></use></svg></span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/docs/assets/js/app.cdec4db9.js" defer></script><script src="/docs/assets/js/2.993bd611.js" defer></script><script src="/docs/assets/js/85.e9d7c0d1.js" defer></script><script src="/docs/assets/js/7.5a12abef.js" defer></script>
  </body>
</html>
